package com.mindainfo.groundwave.web.member.web;

import com.mindainfo.groundwave.common.api.member.param.MemberChangePasswordParam;
import com.mindainfo.groundwave.common.api.member.param.MemberCreateMemberParam;
import com.mindainfo.groundwave.common.api.member.param.MemberQueryListParam;
import com.mindainfo.groundwave.common.api.member.param.MemberSaveParam;
import com.mindainfo.groundwave.common.api.member.service.IDeptService;
import com.mindainfo.groundwave.common.api.member.service.IMemberService;
import com.mindainfo.groundwave.common.api.member.service.IRoleService;
import com.mindainfo.groundwave.common.api.member.vo.*;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Created by MD-01 on 2017/5/17.
 */
@Controller
@RequestMapping("/sys/member")
public class MemberController {

    @Resource
    private IMemberService memberService;
    @Resource
    private IRoleService roleService;
    @Resource
    private IDeptService deptService;

    @RequestMapping("/page")
    public String page(HttpServletRequest request){

        CommonResult<List<RoleQueryUsableAllVo>> roleResult = roleService.queryUsableAll();
        CommonResult<List<DeptGetMyDeptsVo>> deptResult = deptService.getMyDepts();
        request.setAttribute("depts",deptResult.getData());
        request.setAttribute("roles",roleResult.getData());
        return "/member/member";
    }

    @RequestMapping("/save")
    @ResponseBody
    public CommonResult<Void> save(Integer memberId,String username,
                                   Integer usable, String roleId){

        MemberSaveParam param = new MemberSaveParam();
        param.setMemberId(memberId);
        param.setUsable(usable);
        param.setUsername(username);
        if(!StringUtils.isEmpty(roleId)){
            String[] idArray = roleId.split(",");
            for(String str : idArray){
                param.getRoleIdList().add(Integer.parseInt(str));
            }
        }
        return memberService.save(param);
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public TableData<MemberQueryListVo> queryList(Integer start, Integer length, String draw,String key){

        MemberQueryListParam param = new MemberQueryListParam();
        param.setStart(start);
        param.setNum(length);
        param.setKey(key);
        CommonResult<TableData<MemberQueryListVo>> result = memberService.queryList(param);
        result.getData().setDraw(draw);
        return result.getData();
    }

    @RequestMapping("/changePassword")
    @ResponseBody
    public CommonResult<Void> changePassword(Integer memberId,String oldPassword,String newPassword){

        MemberChangePasswordParam param = new MemberChangePasswordParam();
        param.setMemberId(memberId);
        param.setOldPassword(oldPassword);
        param.setNewPassword(newPassword);
        return memberService.changePassword(param);
    }

    @RequestMapping("/createMember")
    @ResponseBody
    public CommonResult<Void> createMember(Integer employeeId,String username,
                                           String password,Integer deptId){

        MemberCreateMemberParam param = new MemberCreateMemberParam();
        param.setDeptId(deptId);
        param.setEmployeeId(employeeId);
        param.setPassword(password);
        param.setUsername(username);
        return memberService.createMember(param);
    }
}
